// https://www.luogu.com.cn/problem/P2890

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    string s;
    cin >> s;

    // cost[c] = min(add, del) for char c
    vector<int> cost(256, INT_MAX); // ASCII

    for (int i = 0; i < n; i++) {
        char c;
        int add, del;
        cin >> c >> add >> del;
        cost[c] = min(add, del);
    }

    // dp[i][j]: min cost to make s[i..j] palindrome
    vector<vector<int>> dp(m, vector<int>(m, 0));

    // 区间长度从 2 到 m
    for (int len = 2; len <= m; len++) {
        for (int i = 0; i <= m - len; i++) {
            int j = i + len - 1;
            if (s[i] == s[j]) {
                dp[i][j] = dp[i+1][j-1]; // 注意：当 len=2 时，i+1 > j-1，此时 dp[i+1][j-1]=0，正确
            } else {
                dp[i][j] = min(
                    dp[i+1][j] + cost[s[i]],
                    dp[i][j-1] + cost[s[j]]
                );
            }
        }
    }

    cout << dp[0][m-1] << endl;
    return 0;
}